package cn.com.se.其他工具;



import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 时间处理帮助类
 */
@SuppressWarnings("deprecation")
public class DateTimeUtils extends DateUtils {

    private static final long MINUTE = 60 * 1000L;// 1分钟

    private static final long HOUR = 60 * MINUTE;// 1小时

    private static final long DAY = 24 * HOUR;// 1天

    private static final long MONTH = 31 * DAY;// 月

    private static final long YEAR = 12 * MONTH;// 年

    /**
     * 默认的时间格式
     */
    public static final String DATE_FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_FORMAT_YEAR = "yyyy";

    public static final String DATE_FORMAT_NOJOINER_MONTH = "yyyyMM";

    public static final String DATE_FORMAT_MONTH = "yyyy-MM";

    public static final String DATE_FORMAT_NOJOINER_DAY = "yyyyMMdd";

    public static final String DATE_FORMAT_DAY = "yyyy-MM-dd";

    public static final String DATE_FORMAT_DAY_EXCEL = "yyyy/MM/dd";

    public static final String DATE_FORMAT_NO_SECOND = "yyyy-MM-dd HH:mm";

    public static final String DATE_FORMAT_EXPORT = "yyyyMMddHHmmss";

    private static String[] parsePatterns = {DATE_FORMAT_DAY, DATE_FORMAT_DEFAULT, DATE_FORMAT_NO_SECOND, DATE_FORMAT_MONTH,
            DATE_FORMAT_DAY_EXCEL, "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
            "yyyy.MM.dd HH:mm", "yyyy.MM", DATE_FORMAT_EXPORT, "yyyyMMddHHmm"};

    /**
     * 获取当前时间字符串
     *
     * @param date 时间点
     * @return String
     */
    public static String formatDateTime(Date date) {
        return DateFormatUtils.format(date, DATE_FORMAT_DEFAULT);
    }

    /**
     * 得到当前时间字符串 格式（HH:mm:ss）
     */
    public static String getTime() {
        return formatDateTime(new Date(), "HH:mm:ss");
    }

    public static String getDate() {
        return formatDateTime(new Date(), DATE_FORMAT_DAY);
    }

    public static String getDate(String format) {
        return formatDateTime(new Date(), format);
    }

    /**
     * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String getDateTime() {
        return formatDateTime(new Date());
    }




    /**
     * 获取指定时间的前一天00:00:00
     *
     * @param date 指定的时间
     * @return String
     */
    public static Date getOffsetMonth(Date date, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, offset);
        return calendar.getTime();
    }

    /**
     * 获取指定时间的前一天00:00:00
     *
     * @param date 指定的时间
     * @return String
     */
    public static Date getOffsetDay(Date date, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, offset);
        return calendar.getTime();
    }

    /**
     * 获取指定时间的相对于小时的偏移量
     *
     * @param date 指定的时间
     * @return String
     */
    public static String getOffsetHours(Date date, int offset, String dateFormat) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, offset);
        Date dateTemp = calendar.getTime();

        return formatDateTime(dateTemp, dateFormat);
    }

    /**
     * 将date转为yyyy-MM-dd HH:mm:ss
     *
     * @param date   需要转化的时间
     * @param format 时间格式
     * @return String String
     */
    public static String formatDateTime(Date date, String format) {
        if (date == null) {
            return null;
        }
        return DateFormatUtils.format(date, format);
    }

    /**
     * 将date转为yyyy-MM-dd HH:mm:ss
     *
     * @param date   需要转化的时间
     * @param format 时间格式
     * @return String String
     */
    public static String formatDate(Date date, String format) {
        if (date == null) {
            return "";
        }
        if (StringUtils.isBlank(format)) {
            return DateTimeUtils.formatDateTime(date);
        }
        return DateFormatUtils.format(date, format);
    }


    /**
     * 获取指定时间的相对于时间类型和偏移量的时间(返回时间字符串)
     *
     * @param date       时间对象
     * @param offsetType 偏移的类型
     * @param offset     偏移量
     * @param dateFormat 时间类型
     * @return 时间字符串
     */
    public static String getOffsetTime(Date date, int offsetType, int offset, String dateFormat) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(offsetType, offset);
        Date dateTemp = calendar.getTime();

        return formatDateTime(dateTemp, dateFormat);
    }

    /**
     * 获取指定时间的相对于时间类型和偏移量的时间(返回时间类型)
     *
     * @param date       时间对象
     * @param offsetType 偏移的类型
     * @param offset     偏移量
     * @return 时间类型
     */
    public static Date getOffsetDate(Date date, int offsetType, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(offsetType, offset);

        return calendar.getTime();
    }

    /**
     * 得到本日的前几天时间 如果number=2当日为2007-9-12,那么获得2007-9-10
     */
    public static String getDateBeforeDay(int number) {
        Date offsetDate = getOffsetDate(new Date(), Calendar.DATE, -number);
        return formatDateTime(offsetDate, DATE_FORMAT_DAY);
    }

    /**
     * 得到本日的前几个月时间 如果number=2当日为2007-9-1,那么获得2007-7
     */
    public static String getDateBeforeMonth(int number) {
        Date offsetDate = getOffsetDate(new Date(), Calendar.MONTH, -number);
        return formatDateTime(offsetDate, DATE_FORMAT_MONTH);
    }

    /**
     * 获取两个日期之间的天数
     *
     * @param before
     * @param after
     * @return
     */
    public static BigDecimal getDaysOfTwoDate(Date before, Date after) {

        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return new BigDecimal(afterTime - beforeTime).divide(new BigDecimal(DAY), 1, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 得到本日的前几年 如果number=2当日为2007,那么获得2005
     */
    public static String getDateBeforeYear(int number) {
        Date offsetDate = getOffsetDate(new Date(), Calendar.YEAR, -number);
        return formatDateTime(offsetDate, DATE_FORMAT_YEAR);
    }

    /**
     * 获取两个日期之间的小时
     *
     * @param before
     * @param after
     * @return
     */
    public static BigDecimal getHoursOfTwoDate(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return new BigDecimal(afterTime - beforeTime).divide(new BigDecimal(HOUR), 2);
    }

    /**
     * 获取两个日期之间的分钟,向下取整
     *
     * @param before
     * @param after
     * @return
     */
    public static BigDecimal getMinutesOfTwoDate(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return new BigDecimal(afterTime - beforeTime).divide(new BigDecimal(MINUTE), BigDecimal.ROUND_DOWN);
    }

    /**
     * 返回系统现在年份中指定月份的天数
     *
     * @param month 月份
     * @return 指定月的总天数
     */
    public static int getMonthDayNum(int month) {
        Date date = new Date();
        int year = getYear(date);
        return getMonthDayNum(year, month);
    }

    /**
     * 返回指定年份中指定月份的天数
     *
     * @param year  年份
     * @param month 月份
     * @return 指定月的总天数
     */
    public static int getMonthDayNum(int year, int month) {
        int[][] day = {{0, 30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return day[1][month];
        } else {
            return day[0][month];
        }
    }

    /**
     * 判断是平年还是闰年
     *
     * @param year
     * @return
     * @author dylan_xu
     * @date Mar 11, 2012
     */
    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400) == 0;
    }

    /**
     * 获得当前时间戳
     *
     * @return
     */
    public static Timestamp getTimeStamp() {
        Date submitDate = new Date();
        return getTimeStampOfDate(submitDate);
    }

    /**
     * 获得时间戳
     *
     * @return
     */
    public static Timestamp getTimeStampOfDate(Date date) {
        return new Timestamp(date.getTime());
    }

    public static String getTimeFormatText(Date date) {
        if (date == null) {
            return null;
        }
        long diff = new Date().getTime() - date.getTime();
        long r;
        if (diff > YEAR) {
            r = (diff / YEAR);
            return r + "年前";
        }
        if (diff > MONTH) {
            r = (diff / MONTH);
            return r + "个月前";
        }
        if (diff > DAY) {
            r = (diff / DAY);
            return r + "天前";
        }
        if (diff > HOUR) {
            r = (diff / HOUR);
            return r + "小时前";
        }
        if (diff > MINUTE) {
            r = (diff / MINUTE);
            return r + "分钟前";
        }
        return "刚刚";
    }

    /**
     * 获取两个日期之间的天数
     *
     * @param before
     * @param after
     * @return
     */
    public static double getDistanceOfTwoDate(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        long days = (afterTime - beforeTime) / DAY;
        return (double) days;
    }

    /**
     * 转换为时间（天,时:分:秒.毫秒）
     *
     * @param timeMillis
     * @return
     */
    public static String formatDateTime(long timeMillis) {
        long day = timeMillis / (24 * 60 * 60 * 1000);
        long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
        long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
        return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
    }

    /**
     * 获取过去的天数
     *
     * @param date
     * @return
     */
    public static long pastDays(Date date) {
        long t = new Date().getTime() - date.getTime();
        return t / (24 * 60 * 60 * 1000);
    }

    /**
     * 获取过去的小时
     *
     * @param date
     * @return
     */
    public static long pastHour(Date date) {
        long t = new Date().getTime() - date.getTime();
        return t / (60 * 60 * 1000);
    }

    /**
     * 获取过去的分钟
     *
     * @param date
     * @return
     */
    public static long pastMinutes(Date date) {
        long t = new Date().getTime() - date.getTime();
        return t / (60 * 1000);
    }

    /**
     * 日期型字符串转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy.MM.dd",
     * "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 闭区间
     *
     * @param date
     * @param start
     * @param end
     * @return
     */
    public static boolean between(Date date, Date start, Date end) {
        if (date.before(start)) {
            return false;
        }
        return !date.after(end);
    }

    public static boolean beforeEqual(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return false;
        }
        return date1.getTime() <= date2.getTime();
    }

    public static boolean afterEqual(Date date1, Date date2) {
        return date1.getTime() >= date2.getTime();
    }

    public static boolean isEqual(Date date1, Date date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 == null || date2 == null) {
            return false;
        }
        return date1.equals(date2);
    }

    public static long getDateTime(Date date, Date time) {
        Calendar dateCal = getCalendar(date);
        Calendar timeCal = getCalendar(time);
        dateCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));
        dateCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));
        dateCal.set(Calendar.SECOND, timeCal.get(Calendar.SECOND));
        dateCal.set(Calendar.MILLISECOND, timeCal.get(Calendar.MILLISECOND));
        return dateCal.getTimeInMillis();
    }

    public static Calendar getCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * 获取两个时间的时间差
     *
     * @param endDate
     * @param nowDate
     * @param timeType 1: 天数  2: 小时数  3: 分钟数
     * @return
     */
    public static long getDatePoor(Date endDate, Date nowDate, int timeType) {
        if (endDate == null || nowDate == null) {
            return 0;
        }
        // 获得两个时间的毫秒时间差
        long diff = endDate.getTime() - nowDate.getTime();
        switch (timeType) {
            case 1: // 计算差多少天
                return diff / DAY;
            case 2: // 计算差多少小时
                return diff / HOUR;
            case 3: // 计算差多少分钟
                return diff / MINUTE;
            default:
                return 0;
        }
    }

    /**
     * 获取司龄
     *
     * @param entryDate 入职时间
     * @return 2016-06-12 至 2017-06-11 返回 0 <br> 2016-06-12 至 2017-06-12 返回 1
     */
    public static int getCompanyAge(Date entryDate) {
        Date currDate = new Date();
        Calendar sdc = getCalendar(entryDate);
        int i = 0;
        while (true) {
            sdc.add(Calendar.YEAR, 1);
            if (sdc.getTime().after(currDate)) {
                return i;
            }
            i++;
        }
    }

    /**
     * 计算两个日期间的工龄
     *
     * @param calDate      参与计算的日期
     * @param workYearDate 最早工作日期
     * @return
     */
    public static int getCompanyAge(Date calDate, Date workYearDate) {
        Calendar sdc = getCalendar(workYearDate);
        int i = 0;
        while (true) {
            sdc.add(Calendar.YEAR, 1);
            if (sdc.getTime().after(calDate)) {
                return i;
            }
            i++;
        }
    }

    /**
     * 获取所比较日期的当前年份日期
     * 即如果比较月日在当前月日之前，则为去年，否则则是当前年月日
     * 比如 比较年月日是：2020年3月1号，当前时间是：2020年2月1号，则返回的是2019年3月1号
     * 如果当前时间是：2020年4月1号，即当年已经过了3月1号，则返回的是2020年3月1号
     *
     * @param compareDate
     * @return
     */
    public static Date getCurrentDateAndYear(Date compareDate) {
        Date currDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String currentYear = sdf.format(currDate); // 当前年份
        sdf = new SimpleDateFormat("MM-dd");
        String compareMonAndDay = sdf.format(compareDate); // 比较年月日
        Date actDate = parseDate(currentYear + "-" + compareMonAndDay);
        if (currDate.getTime() >= actDate.getTime()) {
            return actDate;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(actDate);
        c.add(Calendar.YEAR, -1);
        return c.getTime();
    }


    /**
     * 获取前一个季度
     *
     * @param quarter
     * @return
     */
    public static String getPrevQuarter(String quarter, String year) {
        if ("Q1".equals(quarter)) {
            return (Integer.parseInt(year) - 1) + "Q4";
        } else if ("Q2".equals(quarter)) {
            return year + "Q1";
        } else if ("Q3".equals(quarter)) {
            return year + "Q2";
        } else if ("Q4".equals(quarter)) {
            return year + "Q3";

        }
        return null;
    }

    /**
     * 获取当月第一天
     *
     * @return
     */
    public static Date getCurrentMonthFirstDay() {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, 0);
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        return calendar1.getTime();
    }

    /**
     * 获取季度开始日期
     *
     * @param year    年份
     * @param quarter 季度
     * @return 季度开始日期
     */
    public static Date getQuarterStartDate(String year, String quarter) {
        if ("Q1".equals(quarter)) {
            return DateTimeUtils.parseDate(year + "-1-1");
        } else if ("Q2".equals(quarter)) {
            return DateTimeUtils.parseDate(year + "-4-1");
        } else if ("Q3".equals(quarter)) {
            return DateTimeUtils.parseDate(year + "-7-1");
        } else if ("Q4".equals(quarter)) {
            return DateTimeUtils.parseDate(year + "-10-1");

        }
        return null;
    }

    /**
     * 获取季度结束时间
     *
     * @param year    年份
     * @param quarter 季度
     * @return 季度结束时间
     */
    public static Date getQuarterEndDate(String year, String quarter) {
        if ("Q1".equals(quarter)) {
            return DateTimeUtils.parseDate(year + "-3-31 23:59:59");
        } else if ("Q2".equals(quarter)) {
            return DateTimeUtils.parseDate(year + "-6-30 23:59:59");
        } else if ("Q3".equals(quarter)) {
            return DateTimeUtils.parseDate(year + "-9-30 23:59:59");
        } else if ("Q4".equals(quarter)) {
            return DateTimeUtils.parseDate(year + "-12-31 23:59:59");
        }
        return null;
    }

    /**
     * 获取当月最后一天
     *
     * @return
     */
    public static Date getCurrentMonthLastDay() {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(Calendar.DAY_OF_MONTH, calendar1.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar1.getTime();
    }

    /**
     * 获取指定月的最后一天
     *
     * @return
     */
    public static Date getMonthLastDay(Date date) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);
        calendar1.set(Calendar.DAY_OF_MONTH, calendar1.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar1.getTime();
    }

    /**
     * 获取上一个月份第一天
     *
     * @return
     */
    public static Date getPreMonthFirstDay() {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, -1);
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        return calendar1.getTime();
    }

    /**
     * 获取上一个月份最后一天
     *
     * @return
     */
    public static Date getPreMonthLastDay() {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(Calendar.DAY_OF_MONTH, 0);
        return calendar1.getTime();
    }

    /**
     * 获取两个日期之间的月数差
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getMonth(Date startDate, Date endDate) {

        int iMonth;
        int flag = 0;
        try {
            Calendar c1 = Calendar.getInstance();
            c1.setTime(startDate);
            Calendar c2 = Calendar.getInstance();
            c2.setTime(endDate);
            if (c2.equals(c1)) {
                return 0;
            }
            if (c1.after(c2)) {
                Calendar temp = c1;
                c1 = c2;
                c2 = temp;
            }
            if (c2.get(Calendar.DAY_OF_MONTH) < c1.get(Calendar.DAY_OF_MONTH)) {
                flag = 1;
            }
            if (c2.get(Calendar.YEAR) > c1.get(Calendar.YEAR)) {
                iMonth = ((c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12 + c2.get(Calendar.MONTH) - flag)
                        - c1.get(Calendar.MONTH);
            } else {
                iMonth = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH) - flag;
            }

        } catch (Exception e) {
            return 0;
        }
        return iMonth;
    }

    /**
     * 获取本年的开始时间
     *
     * @return
     */
    public static Date getBeginDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        // cal.set
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);

        return getDayStartTime(cal.getTime());
    }

    /**
     * 获取本年的结束时间
     *
     * @return
     */
    public static Date getEndDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DATE, 31);
        return getDayEndTime(cal.getTime());
    }

    /**
     * 获取某个日期的开始时间
     *
     * @param d
     * @return
     */
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取某个日期的结束时间
     *
     * @param d
     * @return
     */
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取今年是哪一年
     *
     * @return
     */
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(Calendar.YEAR);
    }

    /**
     * 获取当前年份字符串
     *
     * @return
     */
    public static String getCurrentYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        return sdf.format(date);
    }

    /**
     * 获取当前月份字符串
     *
     * @return
     */
    public static String getCurrentMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        Date date = new Date();
        return sdf.format(date);
    }

    /**
     * 获取时间属于哪一年
     *
     * @return
     */
    public static Integer getDateYear(Date date) {
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(Calendar.YEAR);
    }

    /**
     * 获取某年最后一天日期
     *
     * @param yearStr 年份
     * @return Date
     */
    public static Date getCurrYearLast(String yearStr) {
        int year = Integer.parseInt(yearStr);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTime();
    }

    /**
     * 获取指定时间的 年份
     *
     * @param date 指定日期
     * @return
     */
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取指定时间的 月份数
     *
     * @param date 指定日期
     * @return
     */
    public static int getDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取指定时间的 小时数(24)
     *
     * @param date 指定日期
     * @return
     */
    public static int getHourOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取指定时间的 分钟数
     *
     * @param date 指定日期
     * @return
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 获取下周一
     *
     * @param date
     * @return
     */
    public static Date getNextWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, 7);
        return cal.getTime();
    }


    /**
     * 获取本周一
     *
     * @param date
     * @return
     */
    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }


    /**
     * 获取当年的第一天
     *
     * @return
     */
    public static Date getCurrYearFirst() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }

    /**
     * 获取当年的最后一天
     *
     * @return
     */
    public static Date getCurrYearLast() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearLast(currentYear);
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();

        return currYearLast;
    }

    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取当年的上一年
     *
     * @return
     */
    public static String getLastYear() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YEAR);

        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        return format.format(y);
    }


    /**
     * 带时分秒的日期字符串转换
     *
     * @param input        输入的日期
     * @param inputFormat  输入日期的格式
     * @param outputFormat 输出日期的格式
     * @return 输出指定格式的日期，可以带时分秒，也可以不带
     */
    public static String formattedDateTime(String input, String inputFormat, String outputFormat) {
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputFormat);
        LocalDateTime localDateTime = LocalDateTime.parse(input, inputFormatter);

        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputFormat);
        return localDateTime.format(outputFormatter);
    }


    /**
     * 日期字符串转换
     *
     * @param input        输入的日期
     * @param inputFormat  输入日期的格式
     * @param outputFormat 输出日期的格式
     * @return 输出指定格式的日期，可以带时分秒，也可以不带
     */
    public static String formattedDate(String input, String inputFormat, String outputFormat) {
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputFormat);
        LocalDate localDate = LocalDate.parse(input, inputFormatter);

        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputFormat);
        return localDate.format(outputFormatter);
    }


    /**
     * 返回两个时间间隔内的所有日期（正序）
     *
     * @param dateStart
     * @param dateEnd
     * @return java.util.List<java.lang.String>
     * @Title: getTwoDaysDay
     */
    public static List<String> findDates(String dateStart, String dateEnd) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        List<String> dateList = new ArrayList<String>();

        try {
            Date dateOne = sdf.parse(dateStart);
            Date dateTwo = sdf.parse(dateEnd);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateOne);

            dateList.add(sdf.format(dateOne));
            while (calendar.getTime().before(dateTwo)) {
                calendar.add(Calendar.DAY_OF_MONTH, +1);
                dateList.add(sdf.format(calendar.getTime()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateList;
    }

    /**
     * 获取周期开始日期
     */
    public static Date getPeriodStartDate(String period, String year) {
        if ("Q1".equals(period) || "上半年".equals(period) || "年度".equals(period)) {
            return DateTimeUtils.parseDate(year + "-1-1");
        } else if ("Q2".equals(period)) {
            return DateTimeUtils.parseDate(year + "-4-1");
        } else if ("Q3".equals(period) || "下半年".equals(period)) {
            return DateTimeUtils.parseDate(year + "-7-1");
        } else if ("Q4".equals(period)) {
            return DateTimeUtils.parseDate(year + "-10-1");
        }
        return null;
    }

    /**
     * 获取周期结束时间
     *
     * @return
     */
    public static Date getPeriodEndDate(String period, String year) {
        if ("Q1".equals(period)) {
            return DateTimeUtils.parseDate(year + "-3-31 23:59:59");
        } else if ("Q2".equals(period) || "上半年".equals(period)) {
            return DateTimeUtils.parseDate(year + "-6-30 23:59:59");
        } else if ("Q3".equals(period)) {
            return DateTimeUtils.parseDate(year + "-9-30 23:59:59");
        } else if ("Q4".equals(period) || "下半年".equals(period) || "年度".equals(period)) {
            return DateTimeUtils.parseDate(year + "-12-31 23:59:59");
        }
        return null;
    }

}
